package com.example.cardemo.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.cardemo.ModellistActivity
import com.example.cardemo.R
import com.example.cardemo.bean.BrandlistBean
import com.example.cardemo.databinding.ItemBrandlistBinding

class BrandlistAdapter(var ctx: Context, var mDatas: MutableList<BrandlistBean.ResultBean>) : RecyclerView.Adapter<BrandlistAdapter.BrandHolder>() {
    private val inflater = LayoutInflater.from(ctx)

    class BrandHolder(val binding: ItemBrandlistBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BrandHolder {
        val binding: ItemBrandlistBinding = DataBindingUtil.inflate(inflater, R.layout.item_brandlist, parent, false)
        return BrandHolder(binding)
    }

    override fun getItemCount(): Int {
        return mDatas.size
    }

    override fun onBindViewHolder(holder: BrandHolder, position: Int) {
        val bean = mDatas[position]
        holder.binding.item = bean
        holder.binding.root.setOnClickListener {
            val intent = Intent(ctx, ModellistActivity::class.java)
            intent.putExtra("bean", bean)
            Log.d("BrandlistAdapter", "Intent created with bean: $bean")
            ctx.startActivity(intent)
        }
    }
}